#include <stdio.h>

#include <graphics.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#pragma comment(lib,"Winmm.lib")

//游戏画面尺寸

#define High 864

#define Width 591


IMAGE img_bk;                           //背景图片

IMAGE img_planeNormal1,img_planeNormal2;/图片

int p_x,p_y;                            /坐标

IMAGE img_bullet1,img_bullet2;          //子弹图片

int bullet_x,bullet_y;                  //子弹坐标

IMAGE img_enemyPlane1,img_enemyPlane2;  //敌机图片

float enemy_x,enemy_y;                  //敌机坐标

IMAGE img_planeExplode1,img_planeExplode2;  //爆炸图片

int score;



void startup()               //数据初始化

{	

	mciSendString("open D:\\game_music.mp3 alias bkmusic ",NULL,0,NULL);//BGM

	mciSendString("play bkmusic repeat ",NULL,0,NULL);//BGM
	initgraph(Width,High);
	loadimage(&img_bk,"D:\\background.jpg");//读取图片到image
	loadimage(&img_planeNormal1,"D:\\planeNormal_1.jpg");
	loadimage(&img_planeNormal2,"D:\\planeNormal_2.jpg");//飞机图片
	loadimage(&img_bullet1,"D:\\bullet1.jpg");
	loadimage(&img_bullet2,"D:\\bullet2.jpg");//子弹图片	
	loadimage(&img_enemyPlane1,"D:\\enemyPlane1.jpg");
	loadimage(&img_enemyPlane2,"D:\\enemyPlane2.jpg");//敌机图片
	loadimage(&img_enemyPlane1,"D:\\enemyPlane1.jpg");
	loadimage(&img_enemyPlane2,"D:\\enemyPlane2.jpg");//
	loadimage(&img_planeExplode1,"D:\\planeExplode_1.jpg");
	loadimage(&img_planeExplode2,"D:\\planeExplode_2.jpg");//爆炸图片
	p_x=Width*0.5;
	p_y=High*0.5;
	enemy_x=Width*0.5;
	enemy_y=0;
	score=0;
	BeginBatchDraw();

}

void show()  //显示画面

{

	char s[10];

	putimage(0,0,&img_bk);//在坐标（0,0）显示IMAGE对象__背景

	putimage(p_x-51,p_y-70,&img_planeNormal1,NOTSRCERASE);

	putimage(p_x-51,p_y-70,&img_planeNormal2,SRCINVERT);//显示飞机

	putimage(bullet_x,bullet_y,&img_bullet1,NOTSRCERASE);

	putimage(bullet_x,bullet_y,&img_bullet2,SRCINVERT);//显示子弹

	putimage(enemy_x,enemy_y,&img_enemyPlane1,NOTSRCERASE);

	putimage(enemy_x,enemy_y,&img_enemyPlane2,SRCINVERT);//显示敌机

	if(abs(enemy_x-bullet_x) + abs(enemy_y-bullet_y) < 100)//击中敌机并计分

	{

		mciSendString("close gotEnemy",NULL,0,NULL);

		mciSendString("open D:\\gotEnemy.mp3 alias gotEnemy",NULL,0,NULL);

		mciSendString("play gotEnemy",NULL,0,NULL);

		enemy_x=rand()%(Width-40);

		enemy_y=0;

		score++;

	}	
	outtextxy(10,10,"得分");

	sprintf(s,"%d",score);

	outtextxy(10,35,s);
	FlushBatchDraw();

}

void gameover()

{
	putimage(p_x-51,p_y-70,&img_planeExplode1,NOTSRCERASE);

	putimage(p_x-51,p_y-70,&img_planeExplode2,SRCINVERT);//显示爆炸	

	EndBatchDraw();

	getch();

	closegraph();

}
void updateWithoutInput()    //与用户输入无关的更新

{
	if(bullet_y>-25)

	{
		bullet_y-=2;//子弹向上移动

	}

	if(enemy_y<High)

		enemy_y+=0.2;        //敌机自动下落	

	else

	{
		enemy_x=rand()%Width;

		enemy_y=0;

	}

	if((p_x>=enemy_x&&p_x<=enemy_x+104) && (p_y<=enemy_y+148&&p_y>=enemy_y))//撞机

	{
		mciSendString("close explode",NULL,0,NULL);
		mciSendString("open D:\\explode.mp3 alias explode",NULL,0,NULL);
		mciSendString("play explode",NULL,0,NULL);
		gameover();
		system("pause");
		exit(0);
	}
}
void updateWithInput()            //与用户输入有关的更新
{
	MOUSEMSG m;//记录鼠标消息
	while(MouseHit())
	{
		m=GetMouseMsg();
		if(m.uMsg==WM_MOUSEMOVE)
		{
			p_x=m.x;
			p_y=m.y;
		}
		else if(m.uMsg==WM_LBUTTONDOWN)//按下左键，发射子弹
		{
			mciSendString("close f_gun",NULL,0,NULL);
			mciSendString("open D:\\f_gun.mp3 alias f_gun",NULL,0,NULL);
			mciSendString("play f_gun",NULL,0,NULL);
			bullet_x=p_x-10;
			bullet_y=p_y-75;
		}
	}
}
int main() 
{
	startup();  //数据初始化
	while(1)    //游戏循环执行
	{
		show();                 //显示画面		
		updateWithoutInput();   //与用户输入无关的更新
		updateWithInput();      //与用户输入有关的更新
	}
	gameover();
	return 0;
}
